﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using PlanningGIS.Framework;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
////using ESRI.ArcGIS.TOCControl;

namespace PlanningGIS.TocContextMenu.Button
{
  
    internal class UniqueValueLayer : BaseCommand
    {
        IApplication m_Application = null;
        public UniqueValueLayer()
        {
            base.m_caption = "按类别分层显示";
            base.m_toolTip = base.Caption;
        }
        
        public override void OnCreate(object hook)
        {
            m_Application = hook as IApplication;
        }

        public override void OnClick()
        {
            IFeatureLayer pFLayer = (m_Application as IFramework).TOCControl.CustomProperty as IFeatureLayer;
            if (pFLayer == null) return;
           
            IMap pMap = (m_Application as IFramework).MapControl.Map;
            IGroupLayer pGroupLyr = new GroupLayerClass();
           
            pMap.AddLayer(pGroupLyr);
          

            IFeatureRenderer renderer = (pFLayer as IGeoFeatureLayer).Renderer;
            if (!(renderer is IUniqueValueRenderer)) return;
            IUniqueValueRenderer uniqueValueRender = renderer as IUniqueValueRenderer;
            pGroupLyr.Name = pFLayer.Name + "（按"+ uniqueValueRender.Field[0]+"分层）";
            for (int i= 0; i < uniqueValueRender.ValueCount;i++)
            {
               
                string value = uniqueValueRender.Value[i];

                IFeatureLayer newLyr = new FeatureLayer();
                newLyr.FeatureClass = pFLayer.FeatureClass;
                newLyr.Name = pFLayer.Name + "(" + value + ")";
           
                pGroupLyr.Add(newLyr);

                IFeatureLayerDefinition featlyrDefinition = newLyr as IFeatureLayerDefinition;
                featlyrDefinition.DefinitionExpression = uniqueValueRender.Field[0] + "='" + value + "'";
                ESRI.ArcGIS.Display.ISymbol sym = uniqueValueRender.Symbol[value];

                ISimpleRenderer isRenderer = new SimpleRenderer();
                isRenderer.Symbol = sym;
                (newLyr as IGeoFeatureLayer).Renderer = isRenderer as IFeatureRenderer;
            }

              (m_Application as IFramework).TOCControl.Update();

        }
        public override bool Enabled
        {
            get
            {
                return ((m_Application as IFramework).IsMapCtrlactive && ((m_Application as IFramework).TOCControl.CustomProperty is IFeatureLayer )) ? true : false;
            }
        }
        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;

                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }
    }
}